package com.chinasofti.etc.alcohol.web;


import com.chinasofti.etc.alcohol.dao.model.Commodity;
import com.chinasofti.etc.alcohol.service.ICommodityService;
import com.chinasofti.etc.alcohol.service.info.TypeInfo;
import com.chinasofti.etc.alcohol.web.form.TypeForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lucas
 * @since 2019-05-29
 */
@Controller
@Slf4j
public class CommodityController {

    @Autowired
    private ICommodityService iCommodityService;
    @RequestMapping("/")
    public String one(ModelMap modelMap){
        /**
         * 头牌
         */

        Commodity commodity=iCommodityService.getById(22);
        modelMap.addAttribute("one",commodity);

        /**
         * 我们的分类
         */
        List<Commodity> commodity1=iCommodityService.toOne();
        modelMap.addAttribute("whiteSpirit",commodity1);

        /**
         * 新品上市
         */


        List<Commodity> commodity4=iCommodityService.newSorting();
        modelMap.addAttribute("newSorting",commodity4);


        /**
         * 分类排序
         */
        List<Commodity> commodity5=iCommodityService.salesRanking("白酒");
        List<Commodity> commodity2=iCommodityService.salesRanking("葡萄酒");
        List<Commodity> commodity3=iCommodityService.salesRanking("洋酒");
        List<Commodity> commodity6=iCommodityService.salesRanking("啤酒");
        modelMap.addAttribute("salesRanking",commodity5);
        modelMap.addAttribute("salesRanking1",commodity2);
        modelMap.addAttribute("salesRanking2",commodity3);
        modelMap.addAttribute("salesRanking3",commodity6);
        return "index";
    }

    /**
     * 分类搜索
     * @param typeForm
     * @param map
     * @return
     */
    @RequestMapping(value = "/query",method = RequestMethod.GET)
    public String query(TypeForm typeForm, ModelMap map){
        TypeInfo typeInfo=new TypeInfo();
        typeForm.setPageIdx(typeForm.getPageIdx()==null||typeForm.getPageIdx()==0?1:typeForm.getPageIdx());
        typeForm.setPageSize(9);
        typeForm.setStart((typeForm.getPageIdx()-1)*typeForm.getPageSize());
        BeanUtils.copyProperties(typeForm,typeInfo);
        List<Commodity> list=iCommodityService.getBrand(typeInfo);
        Integer countI= iCommodityService.getCount(typeInfo);
        int is=countI;
        int i=(is%9==0?is/9:is/9+1);
        map.addAttribute("count",countI);
        map.addAttribute("type",typeForm);
        map.addAttribute("list",list);
        return "shop";
    }

    @RequestMapping(value = "info",method = RequestMethod.GET)
    public String infoQuery(String commodityNumber,ModelMap map){
        Commodity commodity=iCommodityService.getInfo(commodityNumber);
        map.addAttribute("info",commodity);
        return "single-product";
    }

}

